/*
 This file 'utils' is part of Firebird Integrated Solution 1.0

 Copyright (c) 2018 Lincong

 Contact:  
        Email: lincong1987@gmail.com

        QQ: 159257119
 
 See Usage at http://www.jplatformx.com/firebird

 Create date: 2018-05-07 17:31
 */

define(function (require, exports, module) {

	// "use strict";

	var utils = {};


	/**** IMAGES ***/

	utils.createCanvas = function(){
		 return document.createElement('canvas');
	};

	utils.download = function (domImg, filename) {
		// 创建隐藏的可下载链接
		var eleLink = document.createElement('a');
		eleLink.download = filename;
		eleLink.style.display = 'none';
		// 图片转base64地址
		var canvas = this.createCanvas();
		var context = canvas.getContext('2d');
		var width = domImg.natureWidth;
		var height = domImg.natureHeight;
		context.drawImage(domImg, 0, 0);
		// 如果是PNG图片，则context.toDataURL('image/png')
		eleLink.href = context.toDataURL('image/jpeg');
		// 触发点击
		document.body.appendChild(eleLink);
		eleLink.click();
		// 然后移除
		document.body.removeChild(eleLink);
	};


	module.exports = utils;
});